package com.yjd.pub.settlement.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.fundservice.enums.FundChannelEnum;
import com.yjd.comm.fundservice.enums.LoanApplyStatusEnum;
import com.yjd.comm.fundservice.model.FundChannelModel;
import com.yjd.comm.fundservice.model.FundLoanApplyModel;
import com.yjd.comm.fundservice.service.IFundChannelService;
import com.yjd.comm.fundservice.service.IFundLoanApplyService;
import com.yjd.comm.util.ArithUtil;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.settlement.model.ClearDayFundChannelModel;
import com.yjd.comm.settlement.service.IClearDayFundChannelService;
import com.yjd.pub.settlement.dao.IClearDayFundChannelDao;

/**
 * <b>description</b>：第三方资金渠道日结算表业务实现<br>
 * <b>time</b>：2018-03-08 11:04:24 <br>
 * <b>author</b>：  carl
 */
@Service("clearDayFundChannelService")
public class ClearDayFundChannelServiceImpl extends BaseServiceImpl implements IClearDayFundChannelService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public ClearDayFundChannelModel insert(ClearDayFundChannelModel model) throws Exception {
		return this.clearDayFundChannelDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(ClearDayFundChannelModel model) throws Exception {
		return this.clearDayFundChannelDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.clearDayFundChannelDao.deleteModel(paramMap, true);
	}


	@Override
	public void settlementAll(Long startTime, Long endTime) throws Exception {
		for(FundChannelEnum channelEnum:FundChannelEnum.values()){
			settlementAll((long) channelEnum.getValue(),startTime,endTime);
		}
	}

	@Override
	public void settlementAll(Long fundChannel_id, Long startTime, Long endTime) throws Exception {
        if(fundChannel_id==null||startTime==null||endTime==null){
			FrameUtil.throwBaseException1("请求的参数错误");
		}
		FundChannelModel fundChannelModel = fundChannelService.getModelById(fundChannel_id, DbWREnums.WRITE);
		if(fundChannelModel==null){
			FrameUtil.throwBaseException1("请求的资金渠道不存在");
		}
		// 循环生成结算单
		for( ;startTime<= endTime; ){
			this.settlement(fundChannel_id, startTime);
			// 下一天
			startTime += DateUtil.DAY_IN_SECOND;
		}
	}

	public void settlement(Long fund_Channel_id, Long timeInSecond) throws Exception {
		// 时间格式：yyyyMMdd，如：20170824
		int clear_date = Integer.valueOf(DateUtil.format(new Date(timeInSecond*1000), DateUtil.PATTERN_yyyyMMdd));
		Long currentTime = FrameUtil.getTime(null);
		List<FundLoanApplyModel> loanApplyModels  = fundLoanApplyService.getModelList(FrameUtil.newHashMap("fund_channel",
				fund_Channel_id,"start_time",timeInSecond,"end_time",timeInSecond+86400),DbWREnums.WRITE);
		Double apply_price = 0.0D; //申请金额
		int apply_num = 0; // 申请笔数
		Double pass_price = 0D; // 通过金额
		Integer pass_num = 0; // 通过笔数
		Double refuse_price = 0.0D; // 拒绝金额
		Integer refuse_num = 0;//拒绝笔数
		for(FundLoanApplyModel flm : loanApplyModels){
			apply_price = ArithUtil.add(apply_price,flm.getPrice());
			apply_num +=1;
           if(flm.getStatus().intValue()== LoanApplyStatusEnum.REPAYMENTS.getCode()){
           	//成功
			   pass_price = ArithUtil.add(pass_price,flm.getLoan_price()==null?0:flm.getLoan_price());
			   pass_num +=1;
		   }else if(flm.getStatus().intValue()==LoanApplyStatusEnum.REFUSE.getCode()){
		   	//拒绝
			   refuse_price = ArithUtil.add(refuse_price,flm.getLoan_price()==null?0:flm.getLoan_price());
			   refuse_num +=1;
		   }
		}

		// 如果已经生成过了，则先删除渠道结算单
		clearDayFundChannelDao.deleteSettlementList(FrameUtil.newHashMap("channel_id", fund_Channel_id, "clear_date", clear_date));
		ClearDayFundChannelModel cdf = new ClearDayFundChannelModel();
		cdf.setClear_date(clear_date);
		cdf.setApply_num(apply_num);
		cdf.setApply_price(apply_price);
		cdf.setPass_num(pass_num);
		cdf.setPass_price(pass_price);
		cdf.setRefuse_num(refuse_num);
		cdf.setRefuse_price(refuse_price);
		cdf.setChannel_id(fund_Channel_id);
		cdf.setStatus(0); // 0 待确认   1已确认
		cdf.setCreate_time(currentTime);
		cdf.setUpdate_time(currentTime);
		clearDayFundChannelDao.insertModel(cdf, true);
	}

	@Resource
	private IFundChannelService fundChannelService;

	@Resource
	private IFundLoanApplyService fundLoanApplyService;

	@Resource
	private IClearDayFundChannelDao clearDayFundChannelDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.clearDayFundChannelDao;
	}

}
